/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.infor.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.infor.entity.StoreBudget;
import com.jeeplus.modules.infor.mapper.StoreBudgetMapper;
import com.jeeplus.modules.infor.entity.BudgetExpen;
import com.jeeplus.modules.infor.mapper.BudgetExpenMapper;
import com.jeeplus.modules.infor.entity.BudgetIncome;
import com.jeeplus.modules.infor.mapper.BudgetIncomeMapper;

/**
 * 仓租收支明细Service
 * @author 李超辉
 * @version 2019-02-13
 */
@Service
@Transactional(readOnly = true)
public class StoreBudgetService extends CrudService<StoreBudgetMapper, StoreBudget> {

	@Autowired
	private BudgetExpenMapper budgetExpenMapper;
	@Autowired
	private BudgetIncomeMapper budgetIncomeMapper;
	
	public StoreBudget get(String id) {
		StoreBudget storeBudget = super.get(id);
		storeBudget.setBudgetExpenList(budgetExpenMapper.findList(new BudgetExpen(storeBudget)));
		storeBudget.setBudgetIncomeList(budgetIncomeMapper.findList(new BudgetIncome(storeBudget)));
		return storeBudget;
	}
	
	public List<StoreBudget> findList(StoreBudget storeBudget) {
		return super.findList(storeBudget);
	}
	
	public Page<StoreBudget> findPage1(Page<StoreBudget> page, StoreBudget storeBudget) {
		List<StoreBudget> liat=getListByYue();
		page.setList(liat);
		return page;
	}
	public Page<StoreBudget> findPage2(Page<StoreBudget> page, StoreBudget storeBudget) {
		List<StoreBudget> liat=getListBybsc();
		page.setList(liat);
		return page;
	}

	public Page<StoreBudget> findPage3(Page<StoreBudget> page, StoreBudget storeBudget) {
		List<StoreBudget> liat=getListByyear();
		page.setList(liat);
		return page;
	}
	public Page<StoreBudget> findPage(Page<StoreBudget> page, StoreBudget storeBudget) {
		return super.findPage(page,storeBudget);
	}
	@Transactional(readOnly = false)
	public void save(StoreBudget storeBudget) {
		super.save(storeBudget);
		for (BudgetExpen budgetExpen : storeBudget.getBudgetExpenList()){
			if (budgetExpen.getId() == null){
				continue;
			}
			if (BudgetExpen.DEL_FLAG_NORMAL.equals(budgetExpen.getDelFlag())){
				if (StringUtils.isBlank(budgetExpen.getId())){
					budgetExpen.setBudget(storeBudget);
					budgetExpen.preInsert();
					budgetExpenMapper.insert(budgetExpen);
				}else{
					budgetExpen.preUpdate();
					budgetExpenMapper.update(budgetExpen);
				}
			}else{
				budgetExpenMapper.delete(budgetExpen);
			}
		}
		for (BudgetIncome budgetIncome : storeBudget.getBudgetIncomeList()){
			if (budgetIncome.getId() == null){
				continue;
			}
			if (BudgetIncome.DEL_FLAG_NORMAL.equals(budgetIncome.getDelFlag())){
				if (StringUtils.isBlank(budgetIncome.getId())){
					budgetIncome.setBudget(storeBudget);
					budgetIncome.preInsert();
					budgetIncomeMapper.insert(budgetIncome);
				}else{
					budgetIncome.preUpdate();
					budgetIncomeMapper.update(budgetIncome);
				}
			}else{
				budgetIncomeMapper.delete(budgetIncome);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(StoreBudget storeBudget) {
		super.delete(storeBudget);
		budgetExpenMapper.delete(new BudgetExpen(storeBudget));
		budgetIncomeMapper.delete(new BudgetIncome(storeBudget));
	}
	public List<StoreBudget> getListByYue(){
		return  mapper.getListByYue();
	};
	public List<StoreBudget> getListBybsc(){
		return  mapper.getListBybsc();
	};
	public List<StoreBudget> getListByyear(){
		return  mapper.getListByyear();
	};
}